//
//  AddFriendsViewController.m
//  ChatDemo
//
//  Created by imac on 15/12/20.
//  Copyright © 2015年 imac. All rights reserved.
//

#import "AddFriendsViewController.h"
#import "EaseApplyManager.h"
#import "AddFriendsTableCell.h"
#import "UIViewController+BarItem.h"
@interface AddFriendsViewController () <AddFriendsDelegate>

- (IBAction)addFriendAction:(UIButton *)sender;

@property (weak, nonatomic) IBOutlet UITextField *addFrendsTextField;

@property (weak, nonatomic) IBOutlet UITableView *mainTable;

@property (strong, nonatomic) NSMutableArray * applyArray;

@end

@implementation AddFriendsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setBackBtnItem];
    [self setInutStyle:self.addFrendsTextField imgName:@"user"];
    
    
    [self tableViewFooterLineHidden:self.mainTable];
    
    self.applyArray = [[NSMutableArray alloc ] init];
    

    
    [self newApply:nil];
    
    self.mainTable.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
}

-(void)setInutStyle:(UITextField *)textfield imgName:(NSString *)imgName{
    UIView *userNameLeftView = [[UIView alloc ] initWithFrame:CGRectMake(0, 0, 40, 40)];
    UIImageView *img = [[UIImageView alloc ] initWithFrame:CGRectMake(10, 10, 18, 18)];
    img.image = [UIImage imageNamed:imgName];
    [userNameLeftView addSubview:img];
    textfield.leftView = userNameLeftView;
    textfield.leftViewMode = UITextFieldViewModeAlways;
    textfield.layer.cornerRadius = 3;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

-(void)newApply:(NSNotification *)note{
    [self loadDataSourceFromLocalDB];
}

- (void)loadDataSourceFromLocalDB{
    self.applyArray = [NSMutableArray arrayWithArray:[[EaseApplyManager shareManager] loadApplyFromDB]] ;
    [self.mainTable reloadData];
}

- (IBAction)addFriendAction:(UIButton *)sender {
    NSString *buddyName = self.addFrendsTextField.text;
    [self.addFrendsTextField resignFirstResponder];
    [self showHudInView:self.view hint:NSLocalizedString(@"friend.sendApply", @"sending application...")];
    AddFriendStatus addFriendStatus =  [[EaseApplyManager shareManager] addFriend:buddyName];
    [self hideHud];

    switch (addFriendStatus) {
        case DidBuddyExist:
              [self showHint:@"已经是好友了"];
            break;
        case HasSendBuddyRequest:{
            [self showHint:@"已经发送申请"];
            break;
        }
        case SendBuddyRequestSuccess:{
            [self showHint:@"发送成功"];
            break;
        }
        case SendBuddyRequestError:{
            [self showHint:@"发送失败"];
            break;
        }
        default:
            break;
    }
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  self.applyArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    AddFriendsTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AddFriendsTableCell"];
    ApplyEntity *entity = self.applyArray[indexPath.row];
    cell.label.text = entity.applicantUsername;
    cell.delegate = self;
    cell.indexPath = indexPath;
    return cell;
}

/**
 *  拒绝添加好友
 *
 *  @param sender
 */
- (void)refuseBtnAction:(NSIndexPath *)sender{
    
    ApplyEntity *entity = self.applyArray[sender.row];
    EMError *error =  [[EaseApplyManager shareManager] applyRefuseFriend:entity];
    
    if (error == nil) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"setupUntreatedApplyCount" object:nil];
        [self loadDataSourceFromLocalDB];
    }
}

/**
 *  接受好友申请
 *
 *  @param sender
 */
- (void)acceptBtnAction:(NSIndexPath *)sender{
    ApplyEntity *entity = self.applyArray[sender.row];
    EMError *error =  [[EaseApplyManager shareManager] applyAddFriend:entity];
    if (error == nil) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"setupUntreatedApplyCount" object:nil];
        [self loadDataSourceFromLocalDB];
    }
}



@end
